package org.framework.lazy.cloud.network.heartbeat.client.netty.proxy.http.advanced;

import lombok.extern.slf4j.Slf4j;
import org.framework.lazy.cloud.network.heartbeat.client.config.NettyClientProperties;
import org.framework.lazy.cloud.network.heartbeat.client.netty.proxy.http.socket.NettyHttpServerProxyClientRealSocket;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.HandleChannelTypeAdvanced;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.payload.NettyChannelContext;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.payload.NettyProxyMsg;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.proxy.http.client.AbstractHandleHttpDistributeServerProxyClientConnectionSuccessfulTypeAdvanced;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Role;
import org.wu.framework.spring.utils.SpringContextHolder;

import java.util.ArrayList;
import java.util.List;

@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
@Slf4j
public class ClientHandleDistributeHttpServerProxyClientConnectionSuccessfulTypeAdvanced extends
        AbstractHandleHttpDistributeServerProxyClientConnectionSuccessfulTypeAdvanced<NettyProxyMsg> {

    /**
     * 处理当前数据
     *
     * @param nettyChannelContext       当前通道
     * @param nettyProxyMsg 通道数据
     */
    @Override
    protected void doHandler(NettyChannelContext nettyChannelContext, NettyProxyMsg nettyProxyMsg) {


        // 创建一个传输通道
        byte[] msgClientId = nettyProxyMsg.getClientId();
        byte[] msgClientTargetIp = nettyProxyMsg.getTargetIp();
        byte[] msgClientTargetPort = nettyProxyMsg.getTargetPort();
        byte[] msgVisitorId = nettyProxyMsg.getVisitorId();

        String clientId = new String(msgClientId);
        String clientTargetIp = new String(msgClientTargetIp);
        Integer clientTargetPort = Integer.parseInt(new String(msgClientTargetPort));
        String visitorId = new String(msgVisitorId);

        NettyClientProperties nettyClientProperties = SpringContextHolder.getBean(NettyClientProperties.class);
        List<HandleChannelTypeAdvanced> handleChannelTypeAdvancedList = new ArrayList<>(SpringContextHolder.getApplicationContext().getBeansOfType(HandleChannelTypeAdvanced.class).values());

        NettyHttpServerProxyClientRealSocket.buildRealServer(
                clientId,
                clientTargetIp,
                clientTargetPort,
                visitorId,
                nettyClientProperties,
                handleChannelTypeAdvancedList
        );

    }
}
